package cn.lccabc.workflow.service;

import cn.lccabc.workflow.dao.ReAttendMapper;
import cn.lccabc.workflow.entity.ReAttend;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by L on 2018/4/23.
 */
@Service(value = "reAttendServiceImpl")
public class ReAttendServiceImpl implements ReAttendService {

    private static final java.lang.String RE_ATTEND_FLOW_ID = "re_attend";
    private static final Byte RE_ATTEND_STATUS_ONGOING = 1;
    private static final Byte RE_ATTEND_STATUS_PASS = 2;
    private static final Byte RE_ATTEND_STATUS_REFUSE = 3;

    /**
     * 任物关联补签数据链
     */
    private static final String RE_ATTEND_SIGN = "re_attend";
    /**
     * 流程下一步处理人
     */
    private static final String NEXT_HANDLER = "next_handler";

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ReAttendMapper reAttendMapper;

    @Override
    @Transactional
    public void startReAttendFlow(ReAttend reAttend) {
//        ProcessInstance instance = runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID,varibles);
//        System.out.println(instance.getId() + "-----" + instance.getActivityId());
//        Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
//        System.out.println(task.getId() + "---" + task.getName());
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("attend_morning","09:00");
//        map.put("attend_evening","18:30");
//        taskService.complete(task.getId(), map);
        //重写后:
        //重公司组织架构中查询此人上级领导用户名
        reAttend.setCurrent_handler("laoli666");
        reAttend.setStatus(RE_ATTEND_STATUS_ONGOING);
        //插入数据库补签表
        reAttendMapper.insertSelective(reAttend);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(RE_ATTEND_SIGN, reAttend);
        map.put(NEXT_HANDLER,reAttend.getCurrent_handler());
        //启动补签流程实列
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID, map);
        //提交用户补签任务
        Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
        taskService.complete(task.getId(), map);
    }

    @Override
    public List<ReAttend> listTasks(String username) {
//        List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("re_attend_approve").active().list();
//        Map<String, Object> param = taskService.getVariables(tasks.get(0).getId());
//        return tasks;
        //重写后
        List<ReAttend> reAttends = new ArrayList<ReAttend>();
        List<Task> tasks = taskService.createTaskQuery().processVariableValueEquals(username).list();
        //转换成页面实体
        if (CollectionUtils.isNotEmpty(tasks)){
            for (Task task : tasks){
                Map<String, Object> variable = taskService.getVariables(task.getId());
                ReAttend reAttend = (ReAttend) variable.get(RE_ATTEND_SIGN);
                reAttend.setTaskId(task.getId());
                reAttends.add(reAttend);
            }
        }
        return reAttends;
    }

    @Override
    @Transactional
    public void approve(ReAttend reAttend) {
//        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        taskService.complete(taskId);
        //重写后
        Task task = taskService.createTaskQuery().taskId(reAttend.getTaskId()).singleResult();
        if (RE_ATTEND_STATUS_PASS.toString().equals(reAttend.getApproveFlag())){
            //审批通过 修改补签状态
            reAttend.setStatus(RE_ATTEND_STATUS_PASS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        } else if (RE_ATTEND_STATUS_REFUSE.toString().equals(reAttend.getApproveFlag())){
            reAttend.setStatus(RE_ATTEND_STATUS_REFUSE);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }
        taskService.complete(reAttend.getTaskId());
    }
}
